package com.cskaoyan.service;

import com.cskaoyan.bean.Log;
import com.cskaoyan.bean.LogExample;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.log.bo.LogReq;
import com.cskaoyan.mapper.LogMapper;
import com.cskaoyan.util.CskaoyanPageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    /**
     * 分页查询日志
     * @param req
     * @return
     */
    @Override
    public BaseData<Log> getLogList(LogReq req) {
        CskaoyanPageHelper.startPage(req);

        String name = req.getName();
        LogExample example = new LogExample();
        example.setOrderByClause(req.getSort() + " " + req.getOrder());
        LogExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (StringUtils.hasText(name)) {
            criteria.andAdminLike("%" + name + "%");
        }

        List<Log> logs = logMapper.selectByExample(example);
        PageInfo<Log> logPageInfo = new PageInfo<>(logs);
        long total = logPageInfo.getTotal();
        return BaseData.ok(logs, total);
    }

    /**
     * 插入日志切面 封装的日志信息
     * @param log
     */
    @Override
    public void createLog(Log log) {
        Date now = new Date();
        log.setAddTime(now);
        log.setUpdateTime(now);

        logMapper.insertSelective(log);
    }
}
